package com.dzy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dzy.entity.UserFollow;
import com.dzy.entity.UserInfo;
import com.dzy.result.Result;
import com.dzy.service.UserFollowService;
import com.dzy.vo.UserFollowVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/userFollow")
public class UserFollowController {
    @Reference
    private UserFollowService userFollowService;
    /**
     * 关注房源
     * @param houseId
     * @param request
     * @return
     */
    @GetMapping("/auth/follow/{houseId}")
    public Result follow(@PathVariable Long houseId, HttpServletRequest request){
        UserFollow userFollow=new UserFollow();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("USER");
        userFollow.setHouseId(houseId);
        userFollow.setUserId(userInfo.getId());
        userFollowService.insert(userFollow);
        return Result.ok();
    }
    @RequestMapping("/auth/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum,@PathVariable Integer pageSize,HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("USER");
        Long userId = userInfo.getId();
        PageInfo<UserFollowVo> page=this.userFollowService.findListPage(pageNum,pageSize,userId);
        return Result.ok(page);
    }

    @RequestMapping("/auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable Long id){
        this.userFollowService.cancelFollow(id);
        return Result.ok();
    }

}
